﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject_SEM3_SecurityServices.Bussiness;
using eProject_SEM3_SecurityServices.ObjectClass;
using eProject_SEM3_SecurityServices.DataAccess;
using System.Data;
namespace eProject_SEM3_SecurityServices.Presentations
{
    public partial class frmEmployeeContract : System.Web.UI.Page
    {
        BTaskOfDepartment _bTaskOfDepartment = null;
        BEmployee _bEmployee = null;
        int departmentId = -1;
        int numberEmployee = 0;
        int contractId = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            _bTaskOfDepartment = new BTaskOfDepartment();
            _bEmployee = new BEmployee();
            if (!IsPostBack)
            {
                if (Request.QueryString["serviceId"] != null && Request.QueryString["numberEmployee"] != null && Request.QueryString["contractId"] != null)
                {
                    try
                    {
                        departmentId = int.Parse(Request.QueryString["serviceId"].ToString());
                        numberEmployee = int.Parse(Request.QueryString["numberEmployee"].ToString());
                        contractId = int.Parse(Request.QueryString["contractId"].ToString());
                        BindData();
                    }
                    catch (Exception)
                    {
                        
                        throw;
                    }
                   
                }
                BindGrid();
            }
        }
        protected void BindData()
        {
            if (departmentId != -1)
            {
                if (_bEmployee.getEmployeeContract(departmentId, numberEmployee) != null)
                {
                    List<OEmployee> listEm = _bEmployee.getEmployeeContract(departmentId, numberEmployee);
                    //grvEmployeeContract.DataSource = _bEmployee.getEmployeeContract(departmentId, numberEmployee);
                    //grvEmployeeContract.DataBind();
                    foreach (OEmployee item in listEm)
                    {
                        string insert = "insert into Employee_Contract VALUES("+item.EmployeeID+","+contractId+")";
                        DConnectSQL.ExecuteNonQuery(insert,CommandType.Text,null);
                        string updateEm = "update Emloyee_Information set status = " + item.Status + " where employeeId = " + item.EmployeeID;
                        DConnectSQL.ExecuteNonQuery(updateEm,CommandType.Text,null);
                    }
                }
            }
        }
        protected void BindGrid()
        {
            string sql = "select * from Employee_Contract where contractId = " + contractId;            
            DataTable tb = new DataTable();
            tb = DConnectSQL.ExecuteSelectCommand(sql, CommandType.Text);
            grvEmployeeContract.DataSource = tb;
            grvEmployeeContract.DataBind();
        }
        protected void grvEmployeeContract_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvEmployeeContract.PageIndex = e.NewPageIndex;
            BindGrid();
        }
    }
}